package com.apanal.qlife.sys.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.sys.dao.GroupMapper;
import com.apanal.qlife.sys.dao.GroupResourceRelMapper;
import com.apanal.qlife.sys.dao.RoleGroupRelMapper;
import com.apanal.qlife.sys.dao.UserGroupRelMapper;
import com.apanal.qlife.sys.model.Group;
import com.apanal.qlife.sys.model.GroupResourceRel;
import com.apanal.qlife.sys.model.RoleGroupRel;
import com.apanal.qlife.sys.model.UserGroupRel;
import com.apanal.qlife.sys.service.GroupService;
import com.apanal.qlife.sys.service.ResourceService;
import com.apanal.qlife.sys.service.RoleService;
import com.apanal.qlife.sys.service.UserService;

/**
 * 群组服务层接口实现类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:36:08
 */
@Service
public class GroupServiceImpl implements GroupService {

	@Autowired
	GroupMapper mapper;

	@Autowired
	ResourceService resourceService;

	@Autowired
	RoleService roleService;

	@Autowired
	UserService userService;

	@Autowired
	GroupResourceRelMapper groupResourceRelMapper;

	@Autowired
	RoleGroupRelMapper roleGroupRelMapper;

	@Autowired
	UserGroupRelMapper userGroupRelMapper;

	@Override
	public String deleteById(Long id) {
		mapper.deleteByPrimaryKey(id);
		// 删除所有旧用户关联数据
		userGroupRelMapper.deletesByGroId(id);
		// 删除所有旧角色关联数据
		roleGroupRelMapper.deletesByGroId(id);
		// 删除所有旧资源关联数据
		groupResourceRelMapper.deletesByGroId(id);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insert(Group record) {
		record.setGroId(IdWorkerFactory.nextId());
		mapper.insert(record);

		// 新增用户群组关联数据
		if (record.getUserIds() != null && !"".equals(record.getUserIds())) {
			// 新增关联数据
			String[] idArray = record.getUserIds().split(",");
			List<UserGroupRel> list = new ArrayList<UserGroupRel>();
			for (String useId : idArray) {
				UserGroupRel rel = new UserGroupRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setUseId(Long.valueOf(useId));
				rel.setInsertBy(record.getInsertBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				userGroupRelMapper.inserts(list);
			}
		}

		// 如果角色id字符串不为空,则新增群组角色关联表数据
		if (record.getRolIds() != null && !"".equals(record.getRolIds())) {
			String[] idArray = record.getRolIds().split(",");
			List<RoleGroupRel> list = new ArrayList<RoleGroupRel>();
			for (String rolId : idArray) {
				RoleGroupRel rel = new RoleGroupRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setRolId(Long.valueOf(rolId));
				rel.setInsertBy(record.getInsertBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				roleGroupRelMapper.inserts(list);
			}
		}

		// 如果资源id字符串不为空,则新增群组资源关联表数据
		if (record.getResourceIds() != null
				&& !"".equals(record.getResourceIds())) {
			String[] idArray = record.getResourceIds().split(",");
			List<GroupResourceRel> list = new ArrayList<GroupResourceRel>();
			for (String resId : idArray) {
				GroupResourceRel rel = new GroupResourceRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setResId(Long.valueOf(resId));
				rel.setInsertBy(record.getInsertBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				groupResourceRelMapper.inserts(list);
			}
		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String insertSelective(Group record) {
		mapper.insertSelective(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Group selectById(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public String updateByIdSelective(Group record) {
		mapper.updateByPrimaryKeySelective(record);

		// 删除所有旧用户关联数据
		userGroupRelMapper.deletesByGroId(record.getGroId());
		// 新增用户群组关联数据
		if (record.getUserIds() != null && !"".equals(record.getUserIds())) {
			// 新增关联数据
			String[] idArray = record.getUserIds().split(",");
			List<UserGroupRel> list = new ArrayList<UserGroupRel>();
			for (String useId : idArray) {
				UserGroupRel rel = new UserGroupRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setUseId(Long.valueOf(useId));
				rel.setInsertBy(record.getLastUpdateBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				userGroupRelMapper.inserts(list);
			}
		}

		// 删除所有旧角色关联数据
		roleGroupRelMapper.deletesByGroId(record.getGroId());
		// 新增角色群组关联数据
		if (record.getRolIds() != null && !"".equals(record.getRolIds())) {
			// 新增关联数据
			String[] idArray = record.getRolIds().split(",");
			List<RoleGroupRel> list = new ArrayList<RoleGroupRel>();
			for (String rolId : idArray) {
				RoleGroupRel rel = new RoleGroupRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setRolId(Long.valueOf(rolId));
				rel.setInsertBy(record.getLastUpdateBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				roleGroupRelMapper.inserts(list);
			}
		}

		// 删除所有旧资源关联数据
		groupResourceRelMapper.deletesByGroId(record.getGroId());
		// 新增群组资源关联数据
		if (record.getResourceIds() != null
				&& !"".equals(record.getResourceIds())) {
			// 新增关联数据
			String[] idArray = record.getResourceIds().split(",");
			List<GroupResourceRel> list = new ArrayList<GroupResourceRel>();
			for (String resId : idArray) {
				GroupResourceRel rel = new GroupResourceRel();
				rel.setRelId(IdWorkerFactory.nextId());
				rel.setGroId(record.getGroId());
				rel.setResId(Long.valueOf(resId));
				rel.setInsertBy(record.getLastUpdateBy());
				list.add(rel);
			}
			if (!list.isEmpty()) {
				groupResourceRelMapper.inserts(list);
			}
		}
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public String updateById(Group record) {
		mapper.updateByPrimaryKey(record);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Group selectRelationById(Long id) {
		Group record = new Group();
		record.setGroId(id);
		Group group = mapper.selectRelation(record).get(0);
		// 倒序排列子集合,使顺序与列表页面、弹出选择页面顺序保持一致
		Collections.reverse(group.getUserList());
		Collections.reverse(group.getRoleList());
		Collections.reverse(group.getResourceList());
		return group;
	}

	@Override
	public PageReturn<Group> selectRelationForPage(Group entity, Page page) {
		int count = mapper.selectRelationForPageCount(entity, page);
		List<Group> list = mapper.selectRelationForPage(entity, page);
		for (Group group : list) {
			group.setUserList(userService.selectByGroId(group.getGroId()));
			group.setRoleList(roleService.selectByGroId(group.getGroId()));
			group.setResourceList(resourceService.selectByGroId(group
					.getGroId()));
		}
		return new PageReturn<Group>(list, count, page, entity);
	}

	@Override
	public String deleteSoft(Long id, Long useId) {
		Group entity = new Group();
		entity.setGroId(id);
		entity.setLastUpdateBy(useId);
		entity.setStatus(Constants.IS_VALID_FALSE);
		mapper.updateByPrimaryKeySelective(entity);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public List<Group> selectAll() {
		return mapper.select(null);
	}

	@Override
	public String updateToEnableOrDisable(Group group) {
		group.setNormal(group.getNormal() == Constants.IS_VALID_TRUE ? Constants.IS_VALID_FALSE
				: Constants.IS_VALID_TRUE);
		mapper.updateByPrimaryKeySelective(group);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public Boolean checkGroupNameExist(Long groId, String groName) {
		Boolean result = false;
		Group group = new Group();
		group.setGroName(groName);
		List<Group> list = mapper.select(group);

		// 群组id大于0,表示修改校验,排除当前群组
		if (groId > 0) {
			for (Group r : list) {
				if (!r.getGroId().equals(groId)) {
					result = true;
					break;
				}
			}
		}
		// 表示新增群组校验
		else {
			result = !list.isEmpty();
		}
		return result;
	}

	@Override
	public List<Group> selectByUseId(Long useId) {
		return mapper.selectByUserId(useId);
	}

	@Override
	public List<Group> selectByIds(String ids) {
		if (ids == null || "".equals(ids)) {
			return null;
		} else {
			Group param = new Group();
			param.setGroIds(ids);
			return mapper.select(param);
		}
	}

}
